public class Demo2 {
    public static void main(String[] args) {
        Thread thread =new Thread(() -> {
            //通过 currentThread()  方法拿到当前线程示例
            //使用 isInterrupted()  方法获得线程内置的标志位（boolean）
           while (!Thread.currentThread().isInterrupted()) {
               System.out.println("running……");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        thread.start();

        //在等待 3s 后，中断线程
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //立即让 sleep 抛出一个 InterruptedException异常，不会再等待，直接唤醒
        thread.interrupt();
    }
}
